import Vue from 'vue'
import VueRouter from 'vue-router'
import { project } from './project'
import NProgress from 'nprogress'
// //  重写路由的push方法--->这个是vue-cli4.x以上的坑
// const routerPush = VueRouter.prototype.push
// VueRouter.prototype.push = function push(location) {
//   return routerPush.call(this, location).catch((error) => error)
// }
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'index',
    component: () => import('@/views/index/index.vue'),
    children: [
      {
        path: 'user/list',
        name: 'UserList',
        component: () => import('@/views/user/userList'),
      },

      {
        path: 'user/divisionalManage', //部门管理
        name: 'DivisionalManage',
        component: () => import('@/views/user/DivisionalManage'),
      },
      {
        path: 'user/roleManage',
        name: 'RoleManage',
        component: () => import('@/views/system/roleManage'),
      },
      {
        path: 'system/menuManage', //菜单管理
        name: 'MenuManage',
        component: () => import('@/views/system/menuManage'),
      },
      {
        path: 'system/buttonManage',
        name: 'ButtonManage',
        component: () => import('@/views/system/buttonManage'),
      },

      {
        path: 'system/indexManage',
        name: 'IndexManage',
        component: () => import('@/views/system/indexManage'),
      },
      {
        path: 'scheme/List',
        name: 'SchemeList',
        component: () => import('@/views/project/schemeList'),
      },
      {
        path: 'Innovation',
        name: 'Innovation', // 创新方案
        component: () => import('@/views/customer/Innovation'),
      },
      {
        path: 'scheme/supplierSolution',
        name: 'SupplierSolution', // 特殊方案
        component: () => import('@/views/customer/supplierSolution'),
      },
      {
        path: 'BaseLineAndLessonLearn',
        name: 'BaseLineAndLessonLearn', // 基础学习
        component: () => import('@/views/customer/BaseLineAndLessonLearn'),
      },
      {
        path: 'sme/manage',
        name: 'SmeManage',
        component: () => import('@/views/project/smeManage'),
      },
      {
        // 当 /user/:id/profile 匹配成功，
        // UserProfile 会被渲染在 User 的 <router-view> 中
        path: 'AddScheme',
        name: 'AddScheme',
        component: () => import('@/views/project/addScheme'),
      },
      {
        path: 'EditScheme',
        name: 'EditScheme',
        component: () => import('@/views/project/EditScheme'),
      },
      {
        path: 'LogManagement',
        name: 'LogManagement',
        component: () => import('@/views/system/LogManage'),
      },
    ],
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/login.vue'),
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/views/login/register'),
  },
  {
    path: '/resetPassword',
    name: 'ResetPassword',
    component: () => import('@/views/login/resetPasssword'),
  },
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes: [...project, ...routes],
})
// 全局路由守卫
router.beforeEach((to, from, next) => {
  NProgress.start()
  // 全局路由守卫
  let isLogin = sessionStorage.getItem('BackLoginStatus') === 'true' // 判断是否登录，本地存储有用户数据则视为已经登录
  console.log(
    'isLogin :>> ',
    isLogin,
    sessionStorage.getItem('BackLoginStatus')
  )
  if (isLogin) {
    if (to.path === '/login') {
      next({ path: '/' })
    }
    next()
  } else {
    if (to.path === '/login') {
      next()
    } else if (to.path === '/register') {
      next()
    } else if (to.path === '/resetPassword') {
      next()
    } else {
      next({ path: '/login' })
    }
  }
})
router.afterEach(() => {
  NProgress.done()
})
export default router
